import wsgiref.handlers
from google.appengine.ext import webapp
import unittest

from datetime import datetime
import frontpage
import gallery
from pixeltoy import model
from pixeltoy import colorlib
from pixeltoy import datelib


class PixelToyTest(unittest.TestCase):

  def testMakeMainPageOnLocalhost(self):
    handler = frontpage.MainPage()
    page = handler.make_page(host='localhost')
    self.failIf('google-analytics' in page)

  def testMakeMainPageInProduction(self):
    handler = frontpage.MainPage()
    page = handler.make_page(host='pixeltoypixeltoy.appspot.com')
    self.failUnless('google-analytics' in page)    

  def testMakeSpectrum(self):
    colors = colorlib._make_spectrum_rgb()
    self.assertEquals(17, len(colors))
    self.assertEquals((255,0,0), colors[0])
    
  def testPaletteSize(self):
    (w,h) = colorlib.palette_size
    self.assertEquals(w * h, len(colorlib.color_names))

  def noDuplicateColors(self):
    self.assertEquals(len(set(colorlib.color_names)), len(colorlib.color_names))

  def testStartingColor(self):
    self.assertEquals('000000', colorlib.color_names[0])
    self.failIf(colorlib.starting_color == None)
    self.assertEquals('0000ff', colorlib.color_names[colorlib.starting_color])

  def testApplyStroke(self):
    pixels = model.make_empty_pixels()
    self.assertEquals(0, ord(pixels.pixels[0]))
    pixels.apply_stroke("c1_0.0_2.0_0.1")
    self.assertEquals(1, ord(pixels.pixels[0]))
    self.assertEquals(0, ord(pixels.pixels[1]))
    self.assertEquals(1, ord(pixels.pixels[2]))
    self.assertEquals(1, ord(pixels.pixels[model.grid_size.width]))

  def testToSnapshot(self):
    pixels = model.make_empty_pixels()
    pixels.to_snapshot()

  def testParseIsodate(self):
    date = datelib.from_isodate("2001-02-03T04:05:06.000007Z")
    self.assertEquals(2001, date.year)
    self.assertEquals(2, date.month)
    self.assertEquals(3, date.day)
    self.assertEquals(4, date.hour)
    self.assertEquals(5, date.minute)
    self.assertEquals(6, date.second)
    self.assertEquals(7, date.microsecond)
    self.assertEquals(None, date.tzinfo)
    date_string = datelib.to_isodate(date)
    self.assertEquals("2001-02-03T04:05:06.000007Z", date_string)

  def testRoundtripFromCurrentTimeToIsodate(self):
    now = datetime.utcnow()
    now_string = datelib.to_isodate(now)
    after = datelib.from_isodate(now_string)
    self.assertEquals(now, after)
    after_string = datelib.to_isodate(after)
    self.assertEquals(now_string, after_string)
    

class SelfTestPage(webapp.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    runner = unittest.TextTestRunner(stream=self.response.out)
    module = __import__(__name__)
    tests = unittest.defaultTestLoader.loadTestsFromModule(module)
    runner.run(tests)


def main():
  application = webapp.WSGIApplication([('/selftest', SelfTestPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

  
if __name__ == "__main__":
  main()
